package finalproject.mobapp.Main;

import java.util.HashMap;
import java.util.Iterator;

import finalproject.mobapp.R;
import finalproject.mobapp.ARPackage.Poi;
import finalproject.mobapp.ARPackage.PoiRetrieval;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class ARListViewActivity extends Activity {
	
	Context context = this;
	LocationManager locationManager;
	Location gps_loc;
	Location net_loc;
	boolean gps_enabled = false;
	boolean network_enabled = false;
	
	Bundle extras;
	String category;
	int radius = 0;
	Location myLocation;
	
	TextView tv_poiList;
	String poi_list;
	
	static final private int MENU_ARVIEW = Menu.FIRST;
	static final private int MENU_MAPVIEW = Menu.FIRST + 1;
	
	/** Called when the activity is first created. */	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
        
        tv_poiList = (TextView) findViewById(R.id.poilist);
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        
        gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        
        if (gps_enabled) {
        	gps_loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        	if (gps_loc != null) {
				myLocation = gps_loc;
			}
        }
        
        if (network_enabled) {
        	net_loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        	if (net_loc != null) {
				if (gps_loc == null) {
					myLocation = net_loc;
				}
			}
        }
        
        extras = getIntent().getExtras();
        if (extras != null) {
        	category = extras.getString("category");
        	radius = extras.getInt("radius");
        	//Toast.makeText(getApplicationContext(), category + " " + String.valueOf(distance), Toast.LENGTH_LONG).show();
        	updateNearbyPoiList();
        	
        	locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsListener);
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkListener);
        } else {
        	//Toast.makeText(getApplicationContext(), "extras kosong", Toast.LENGTH_LONG).show();
        }
    }
	
	@Override
	public void onStop() {
		super.onStop();
		locationManager.removeUpdates(gpsListener);
		locationManager.removeUpdates(networkListener);
	}
	
	public void updateNearbyPoiList() {
		HashMap<Integer, Poi> nearbyPois = new HashMap<Integer, Poi>();
		nearbyPois = PoiRetrieval.GetNearbyPois(category, radius, myLocation);
		
		poi_list = "";
		if (nearbyPois.size() > 0) {
			Iterator<Integer> e = nearbyPois.keySet().iterator();
			do {
				int poi_id = e.next();
				Poi poi = nearbyPois.get(poi_id);
				Location poi_location = poi.getLocation();
				
				poi_list += poi.getTitle() + "("+ String.valueOf(poi_location.getLatitude()) +","+ String.valueOf(poi_location.getLongitude()) +")\n";
			} while (e.hasNext());
		}
		tv_poiList.setText(poi_list);
	}
	
	LocationListener gpsListener = new LocationListener() {
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			gps_loc = location;
			if (gps_loc != null) {
				myLocation = gps_loc;
				updateNearbyPoiList();
			}
		}
	};
	
	LocationListener networkListener = new LocationListener() {
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			net_loc = location;
			if (net_loc != null) {
				myLocation = net_loc;
				updateNearbyPoiList();
			}
		}
	};
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	menu.add(0, MENU_ARVIEW, Menu.NONE, "AR View");
        menu.add(0, MENU_MAPVIEW, Menu.NONE, "Map View");
        return true;
    }
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		
		Intent intent = new Intent();
		
		switch (item.getItemId()) {
		case (MENU_ARVIEW):
			// Start the ARViewActivity
        	intent.setClass(context, ARViewActivity.class);
        	intent.putExtra("category", category);
        	intent.putExtra("radius", radius);
    		
    		ARListViewActivity.this.finish();
        	startActivity(intent);
        	return true;
        	
		case (MENU_MAPVIEW):
			// Start the MapViewActivity
        	intent.setClass(context, MapViewActivity.class);
        	intent.putExtra("category", category);
        	intent.putExtra("radius", radius);
    		
    		ARListViewActivity.this.finish();
        	startActivity(intent);
        	return true;

		default:
			break;
		}
		
		// Return false if you have not handled the menu item.
		return false;
    }
}
